package com.ddd.test.repoimpl;

import com.ddd.test.aggregate.aggrsales.SalesEntity;
import com.ddd.test.aggregate.aggrsales.entity.SalesAreaEntity;
import com.ddd.test.dao.SalesAreaMapper;
import com.ddd.test.dao.SalesMapper;
import com.ddd.test.dbo.SalesAreaDO;
import com.ddd.test.dbo.SalesDO;
import com.ddd.test.repo.SalesRepo;
import com.ddd.test.utils.SnowFlakeGenerator;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * SalesRepo实现类
 *
 */
@Component(value = "salesRepo")
public class SalesRepoImpl implements SalesRepo {

  @Autowired
  private SalesMapper salesMapper;
  
  @Autowired
  private SalesAreaMapper salesAreaMapper;

  /**
   * 
   */
  @Transactional
  @Override
  public void remove(SalesEntity salesEntity) {
	  salesMapper.deleteBySalesCode(salesEntity.getCode());
	  salesAreaMapper.deleteBySalesCode(salesEntity.getCode());
  }

  /**
   * 
   */
  @Transactional
  @Override
  public void add(SalesEntity salesEntity) {
	if (hasSalesEntity(salesEntity)) {
		salesMapper.updateBySalesCode(convert2SalesDO(salesEntity));
		for(SalesAreaEntity salesAreaEntity : salesEntity.getSalesAreaEntityList()){  
			salesAreaMapper.updateByID(convert2SalesAreaDO(salesEntity,salesAreaEntity));
		}
	}else {		
		salesMapper.insert(convert2SalesDO(salesEntity));
		for(SalesAreaEntity salesAreaEntity : salesEntity.getSalesAreaEntityList()){  
			salesAreaMapper.insert(convert2SalesAreaDO(salesEntity,salesAreaEntity));
		}
	}   
  }

  /**
   * 
   */
  @Override
  public SalesEntity getSalesEntity(String code) {
	SalesDO salesDo = salesMapper.selectBySalesCode(code);
	List<SalesAreaDO> salesAreaDoList = salesAreaMapper.selectBySalesCode(code);	
	if (salesDo != null) {
		return reconstructor(salesDo,salesAreaDoList);
	}else {
		return null;
	}
	
  }

  @Override
  public boolean hasSalesEntity(SalesEntity salesEntity) {
	Integer i= salesMapper.selectCountBySalesCode(salesEntity.getCode());
	if(i>0) {
		return true;
	} else {
		return false;
	}
  }

  /**
   * 
   */
  @Override
  public SalesEntity getSalesEntityByName(String name) {
	SalesDO salesDO = salesMapper.selectBySalesName(name);
	if (salesDO != null) {
		List<SalesAreaDO> salesAreaDOList = salesAreaMapper.selectBySalesCode(salesDO.getCode());
		return reconstructor(salesDO,salesAreaDOList);
	}
	return null;
  }
  
  //重建实体
  private SalesEntity reconstructor(SalesDO salesDO , List<SalesAreaDO> salesAreaDOList) {
	  List<String> salesAreaList = salesAreaDOList.stream().map(SalesAreaDO::getArea).collect(Collectors.toList());
	  SalesEntity salesEntity = SalesEntity.createSalesEntity(salesDO.getCode(), salesDO.getName(), salesDO.getDescription(), 
			  salesDO.getLevel(), salesAreaList);
	  return salesEntity;
  }
  
  //TODO
  private SalesDO convert2SalesDO(SalesEntity salesEntity) {
	  SalesDO salesDo = salesMapper.selectBySalesCode(salesEntity.getCode());
	  if(salesDo == null) {
		  salesDo = new SalesDO();
		  SnowFlakeGenerator snowFlakeGenerator = new SnowFlakeGenerator(1023);
		  salesDo.setId(snowFlakeGenerator.nextId());
		  salesDo.setCode(salesEntity.getCode());
	  }	  	  
	  salesDo.setName(salesEntity.getName());
	  salesDo.setLevel(salesEntity.getLevel());
	  salesDo.setDescription(salesEntity.getDescription());	  
	  return salesDo;
  }

  //TODO
  private SalesAreaDO convert2SalesAreaDO(SalesEntity salesEntity,SalesAreaEntity salesAreaEntity) {
	  SalesAreaDO salesAreaDo = new SalesAreaDO();
	  salesAreaDo.setArea(salesAreaEntity.getAreaName());
	  salesAreaDo.setSalesCode(salesEntity.getCode());
	  
	  SnowFlakeGenerator snowFlakeGenerator = new SnowFlakeGenerator(1023);
	  salesAreaDo.setId(snowFlakeGenerator.nextId());
	  return salesAreaDo;
  }
}